/*
 * Created on Dec 17, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.unash.util;

/**
 * @author Ashwini Gokhale
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

import java.io.IOException;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ScratchPad implements Consts{

	public static void main(String[] args)
	throws IOException, SAXException{
		DOMParser parser = new DOMParser();
		parser.parse(DATABASE_MAP_FILE_NAME);
		Document document = parser.getDocument();
		
		NodeList databaseNodes = document.getElementsByTagName(DATABASE);
		for(int i=0;i<databaseNodes.getLength();i++){
			Node databaseNode = databaseNodes.item(i);
			parseDatabase(databaseNode);
		}//for
	}//main
		
		
	public static void parseDatabase(Node databaseNode){
		//attributes
		NamedNodeMap attrDB = databaseNode.getAttributes();
		System.out.println(attrDB.getNamedItem(DATABASE_ATTRIBUTE_NAME).getNodeValue());
		
		NodeList databaseChildren = databaseNode.getChildNodes();
		int noNodesInTable = databaseChildren.getLength();
		for(int i=0;i<noNodesInTable;i++){
			if(databaseChildren.item(i).getNodeName().equals(TABLE)){
				NodeList tablesNode = (NodeList)databaseChildren.item(i);
				parseTable(tablesNode);
			}//if
		}//for
	}//parseDatabase

	
	public static void parseTable(NodeList tablesNode){
		//System.out.println(((Node)tablesNode).getNodeName());
		//Attributes	
		NamedNodeMap attrTable = ((Node)tablesNode).getAttributes();
		System.out.println(attrTable.getNamedItem(TABLE_ATTRIBUTE_NAME).getNodeValue());
	
		int noNodesInUse = tablesNode.getLength();
		for(int i=0;i<noNodesInUse;i++){
			if(tablesNode.item(i).getNodeName().equals(USE)){
					NodeList useNode = (NodeList)tablesNode.item(i);
					parseUse(useNode);
			}//if	
		}//for
	}//parseTable
	
	
	public static void parseUse(NodeList useNode){
		//System.out.println(((Node)useNode).getNodeName());
		
		//attributes
		NamedNodeMap attrUse = ((Node)useNode).getAttributes();
		System.out.println(attrUse.getNamedItem(USE_ATTRIBUTE_VALUE).getNodeValue());
										
		int noUse = useNode.getLength();
		for(int i=0;i<noUse;i++){
			if((useNode.item(i).getNodeName().equals(COLUMN)) 
			|| (useNode.item(i).getNodeName().equals(DATATYPE))){
				//System.out.println(useNode.item(i).getNodeName());
				System.out.println(useNode.item(i).getChildNodes().item(0).getNodeValue());
			}//if										
		}//for
	}//parseUse
}//scratchPad


	

	

//find @attrset bib-1 @and @or @attr 1=1 one @attr 1=2 two @and @attr 1=3 three @attr 1=4 four
